<?php
class SettingAction extends CommonAction {
    public function index(){
		$this->listmap=1;
		$setting_list=M('Setting_group')->order('sort')->select();
		foreach($setting_list as $k=>$v){
			$_select=$this->dao->where(array('group_id'=>$v['id']))->order('sort')->select();
			foreach($_select as $kk=>$vv){
				$_select[$kk]['_select']=json_decode($vv['_select']);
			}
			$setting_list[$k]['list']=$_select;
		}
		$this->setting_list=$setting_list;
		$cur_setting=$this->dao->select();
		//dump($cur_setting);
		//$this->cur_setting=array();
		foreach($cur_setting as $k=>$v){
			$thi[$v['name']]=$v['val'];
		}
		//是否指定模板ID,如果是则读取当前模板配置
		if($this->_get('template_id')){
			$cur_tem=array();
			$cur_template=M('Template')->where(array('id'=>$this->_get('template_id')))->find();
			foreach(json_decode($cur_template['config'],true) as $k=>$v){
				//遍历所有配置项
				foreach($v['list'] as $kk=>$vv){
					$cur_tem[$vv['name']]=$vv['name'];
				}
			}
			$this->assign('cur_template_id',$cur_template['id']);
			$this->assign('cur_template',$cur_tem);
		}
		$this->cur_setting=$thi;
		$this->display();
	}
	
	
	
	public function edit(){
		$this->group_list=M('Setting_group')->select();
		parent::edit();
	}
	
	/*public function update(){
		if(isset($_POST)){
			$db=M('Setting');
			foreach($_POST as $k=>$v){
			    $db->where(array('name'=>$k))->save(array('val'=>$v));
			}
			$this->success('保置成功!',U('Setting/index'));
		}
	}*/
	//添加方法重写
	public function add(){
		$this->group_list=M('Setting_group')->select();
		parent::add();
	}
	
	
	//生成模板配置列表
	public function createConfList(){
		if($this->_post('ids')){
			$s=array();
			$setting_list=M('Setting_group')->order('sort')->select();
			foreach($setting_list as $k=>$v){
				$_select=$this->dao->where(array('group_id'=>$v['id'],'id'=>array('in',$this->_post('ids'))))->order('sort')->select();
				foreach($_select as $kk=>$vv){
					$_select[$kk]['_select']=json_decode($vv['_select'],true);
				}//dump($_select);
				$setting_list[$k]['list']=$_select;
				if(empty($_select)){
					unset($setting_list[$k]);
				}else{
				    $s[]=$setting_list[$k];
				}
			}
			echo JSON($s);
		}
	}
}
?>